import axios from "axios";

// 创建实例，给定基础配置
const request = axios.create({
  baseURL: "",
  timeout: import.meta.env.VITE_API_TIMEOUT,
});

// 请求拦截器
request.interceptors.request.use((config) => {
  return config;
});

request.interceptors.response.use(
  (resp) => {
    return resp.data;
  },
  (err) => {
    const statusCode = err.response.status;
    const statusMsg: { [key: number]: string } = {
      401: "TOKEN过期",
      403: "无权访问",
      404: "请求地址错误",
      500: "服务器错误",
    };

    const msg = statusMsg[statusCode] || "网络错误，请重试";
    // 错误处理
    console.log(msg);

    return Promise.reject(err);
  },
);

export default request;
